package com.nongcai159.mall.lib_common.base

import android.app.Activity
import android.text.TextUtils

import com.nongcai159.mall.lib_common.utils.MLog
import java.util.Stack

/**
 * activity管理栈
 *
 *
 * Created by tanbiheng on 2018/11/26.
 */

class ActivityStackManager private constructor() {

    private var activityStack: Stack<Activity>? = null

    // 将当前Activity推入栈中
    fun pushActivity(activity: Activity) {
        if (activityStack == null) {
            activityStack = Stack()
        }

        activityStack!!.push(activity)
    }

    // 退出栈顶Activity
    fun popActivity(activity: Activity?) {
        var activity = activity
        if (activity != null) {
            // 在从自定义集合中取出当前Activity时，也进行了Activity的关闭操作
            if (!activity.isFinishing) {
                activity.finish()
            }

            if (activityStack != null) {
                activityStack!!.remove(activity)
            }
            activity = null
        }
    }

    /**
     * 移除但不关闭
     *
     * @param activity
     */
    fun popActivityNoFinish(activity: Activity?) {
        if (activity != null && activityStack != null) {
            activityStack!!.remove(activity)
        }
    }

    /**
     * 移除和关闭指定CLASS的Activity
     *
     * @param cls
     */
    fun popActivityOfClass(cls: Class<*>) {
        if (activityStack != null && !activityStack!!.isEmpty()) {
            for (i in activityStack!!.indices.reversed()) {
                if (activityStack!![i].javaClass == cls) {
                    if (!activityStack!![i].isFinishing) {
                        activityStack!![i].finish()
                    }
                    activityStack!!.removeAt(i)
                    break
                }
            }
        }
    }

    // 获得当前栈底Activity(最早放入的Activity)
    fun firstActivity(): Activity? {
        var activity: Activity? = null
        if (activityStack == null) {
            return null
        }

        if (!activityStack!!.empty()) {
            activity = activityStack!!.firstElement()
        }
        return activity
    }

    // 获得当前栈顶Activity
    fun currentActivity(): Activity? {
        var activity: Activity? = null
        if (activityStack == null) {
            return null
        }

        if (!activityStack!!.empty()) {
            activity = activityStack!!.lastElement()
        }
        return activity
    }

    // 清除除栈底外的所有Activity
    fun clearTaskExceptFirst() {
        if (activityStack != null && !activityStack!!.isEmpty()) {
            for (i in activityStack!!.indices.reversed()) {
                if (!TextUtils.equals(activityStack!![i].javaClass.simpleName,
                        firstActivity()!!.javaClass.simpleName)) {
                    if (!activityStack!![i].isFinishing) {
                        activityStack!![i].finish()
                    }
                    activityStack!!.removeAt(i)
                }
            }
        }
    }

    // 清除除栈底外和栈顶外的所有Activity
    fun clearTaskExceptFirstAndLast() {
        if (activityStack != null && !activityStack!!.isEmpty()) {
            for (i in activityStack!!.size - 2 downTo 0) {
                if (!TextUtils.equals(activityStack!![i].javaClass.simpleName,
                        firstActivity()!!.javaClass.simpleName)) {
                    if (!activityStack!![i].isFinishing) {
                        activityStack!![i].finish()
                    }
                    activityStack!!.removeAt(i)
                }
            }
        }
    }

    /**
     * 退出应用程序
     */
    fun exitActivity() {
        while (true) {
            val activity = firstActivity() ?: break
            popActivity(activity)
        }
    }

    companion object {

        val INSTANCE = ActivityStackManager()

        fun releaseInstance() {
            if (INSTANCE != null) {
                INSTANCE.exitActivity()
                if (INSTANCE.activityStack != null) {
                    INSTANCE.activityStack!!.clear()
                    INSTANCE.activityStack = null
                }
            }
        }
    }
}
